<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * App\IntegralCommodity
 *
 * @mixin \Eloquent
 */
class IntegralCommodity extends Model
{
    public $timestamps = false;
    protected $table = 'commodity';


    const TYPE = 4;

    //提现类型
    const REFLECT_TYPE_WITHDRAW = 0;
    const REFLECT_TYPE_RECHARGE = 3;
    const REFLECT_TYPE_EXCHANGE = 4;
    const REFLECT_TYPE_REAL = 6;

    static $ReflectTypeDisplay = [
        self::REFLECT_TYPE_WITHDRAW => '红包提现',
        self::REFLECT_TYPE_RECHARGE => '话费充值',
        self::REFLECT_TYPE_EXCHANGE => '红包兑换',
        self::REFLECT_TYPE_REAL => '实物兑换',
    ];

    /*
     * 修改积分商品
     */
    static function CommodityModify($request,$integralCommodity)
    {
        $integralCommodity->title = $request->title;
        $integralCommodity->cost_coin_number = $request->cost_coin_number;
        $integralCommodity->recharge_amount = $request->recharge_amount;
        $integralCommodity->reflect_type = $request->reflect_type;
        if ($request->has('photo_url')) {
            $photo_url = Postings::OssUpdate($request->photo_url);
            $integralCommodity->photo_url = Storage::url($photo_url);
        }
        $infos = [];
        for ($i =1; $i <= 9; $i++) {
            $key = 'infos'.$i;
            if ($request->$key) {
                $infos[] = $request->$key;
            }
        }
        $integralCommodity->infos = join(',', $infos);
        if (!$integralCommodity->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }

    /**
     * 添加积分商品
     * @param $request
     * @return array
     */
    static function CommodityPublish($request)
    {
        $integralCommodity = new IntegralCommodity();
        $integralCommodity->title = $request->title;
        $integralCommodity->cost_coin_number = $request->cost_coin_number;
        $integralCommodity->recharge_amount = $request->recharge_amount;
        $integralCommodity->reflect_type = $request->reflect_type;
        $integralCommodity->create_time = date('Y-m-d');
        $integralCommodity->type = self::TYPE;
        if ($request->has('photo_url')) {
            $photo_url = Postings::OssUpdate($request->photo_url);
            $integralCommodity->photo_url = Storage::url($photo_url);
        }
        $infos = [];
        for ($i =1; $i <= 9; $i++) {
            $key = 'infos'.$i;
            if ($request->has($key)) {
                $infos[] = Storage::url(Postings::OssUpdate($request->$key));
            }
        }
        $integralCommodity->infos = join(',', $infos);
        if (!$integralCommodity->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }
}
